package com.lemner.motion.base;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lemner.motion.activity.LoginActivity;
import com.lemner.motion.util.ActivityConllector;
import com.lemner.motion.util.IsInternet;

/**
 * Created by hzq on 2018/3/9.
 */

public abstract class BaseFragment extends Fragment {
    public View view=null;
    public Context context;

    public abstract int getLayoutFile();
    abstract public void initView();
    abstract public void initEvent();
    abstract public void initData();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutFile(), container, false);
        context = getActivity();
        initView();
        initEvent();
        if (!IsInternet.isNetworkAvalible(getActivity())) {
            showToast("当前网络不可用，请检查网络设置");
        }
        else {
            initData();
        }
        return view;
    }

    public void showToast(String msg){
        Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
    }
    //强制下线
    public void forceonline(){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("warning");
        builder.setMessage("您被强制下线, 请您重新登录!");
        builder.setCancelable(false); // 将对话框设置为不可取消
        // 给按钮添加注册监听
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击按钮所调用的方法
                ActivityConllector.finishAll();//销毁所有的活动
                Intent intent1 = new Intent(context, LoginActivity.class);
                context.startActivity(intent1);
            }
        });
        builder.show();
    }
}
